<?php

namespace App\Repository\Auth;

use App\Model\Auth\Menu;
use App\Repository\IRepository;
use Hyperf\Database\Model\Builder;

class MenuRepository extends IRepository
{
    public function __construct(
        protected readonly Menu $model
    )
    {
    }

    public function handleSearch(Builder $query, array $params): Builder
    {
        return $query->when(isset($params['parent_id']) && $params['parent_id'] != "", function ($query) use ($params) {
            return $query->where('parent_id', $params['parent_id']);
        })
            ->when(isset($params['type']) && $params['type'], function ($query) use ($params) {
                return $query->whereIn('type', $params['type']);
            })
            ->when(isset($params['status']) && $params['status'], function ($query) use ($params) {
                return $query->where('status', $params['status']);
            })
            ->when(isset($params['id']) && $params['id'], function ($query) use ($params) {
                if (is_array($params['id'])) {
                    return $query->whereIn('id', $params['id']);
                }
                return $query->where('id', $params['id']);
            });
    }

    public function handleOrderBy(Builder $query, $params): Builder
    {
        return $query->orderBy('sort', 'desc')->orderBy('id', 'desc');
    }
}